﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ThuVien;
namespace TranTekBE.News
{
    public partial class Announcement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            /*
             * Khởi tạo page và set page
             */

            int curPage = 1;
            try
            {
                if (Request.QueryString["page"] != null)
                    curPage = int.Parse(Request.QueryString["page"]);
            }
            catch
            {
                Response.Redirect("Home.aspx");
            }

            /*
             *  Số bài trên 1 page ( 1 row 2 bài)
             */
            int rowsonPage = 4;

            /*
             *  Tổng số bài viết
             */
            int countPost = 0;
            DataSet countsql = FunctionsUtils.Database2.ExecuteQuery("Select * from tbl_News where Loai = 0");
            countPost = countsql.Tables[0].Rows.Count;
            /*
             * Tính số trang
             */

            int _page = (countPost + (rowsonPage - 1)) / rowsonPage;

            /*
             * Lấy bài viết của trang hiện tại
             */

            DataSet curPost = FunctionsUtils.Database2.ExecuteQuery("Select TOP " + rowsonPage + " * from tbl_News where ID not in ( select TOP " + (curPage - 1) * rowsonPage + " ID from tbl_News where Loai = 0 order by ID desc) and Loai = 0 order by ID desc");
            if (curPost != null && curPost.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in curPost.Tables[0].Rows)
                {
                    tbrow.Text += "<article class=\"post six column\">";
                    tbrow.Text += "<div class=\"post-image\">";
                    tbrow.Text += "<a href=\"Read.aspx?id=" + row[0].ToString() + "\"><img src=\"Images/avatar/" + row[7].ToString() + "\" alt=\"" + row[1].ToString() + "\"></a>";
                    tbrow.Text += "</div>";
                    tbrow.Text += "<div class=\"post-container\">";
                    tbrow.Text += "<h2 class=\"post-title\">" + row[1].ToString() + "</h2>";
                    tbrow.Text += "<div class=\"post-content\">";
                    tbrow.Text += "<p>"+row[3].ToString() + "</p>";
                    tbrow.Text += "</div>";
                    tbrow.Text += "</div>";
                    tbrow.Text += "<div class=\"post-meta\">";
                    tbrow.Text += "<span class=\"comments\">" + row[8].ToString() + "</span>";
                    tbrow.Text += "<span class=\"author\">" + row[9].ToString() + "</span>";
                    tbrow.Text += "<span class=\"date\">" + row[5].ToString().Split(' ')[0] + "</span>";
                    tbrow.Text += "<a href=\"Delete.aspx?id=" + row[0].ToString() + "\"><span class=\"del\">Xóa</span></a>";
                    tbrow.Text += "</div>";
                    tbrow.Text += "</article>";
                }
            }
            /*
             * Hiễn thị select page
             */
                pages.Text = "<li class=\"active\"><a href=\"#\">" + curPage + "</a></li>";
                for(int i = 1; i<= 2; i++)
                {
                    if ((curPage + i) <= _page)
                    {
                        pages.Text += "<li><a href=\"Announcement.aspx?page=" + (curPage + i) + "\">" + (curPage + i) + "</a></li>"; 
                    }
                }
                for (int i = 1; i <= 2; i++)
                {
                    if ((curPage - i) >= 1)
                    {
                        pages.Text = "<li><a href=\"Announcement.aspx?page=" + (curPage - i) + "\">" + (curPage - i) + "</a></li>" + pages.Text;
                    }
                }
                if((curPage + 2) < _page)
                {
                    pages.Text += "<li><a>...</a></li>";
                    pages.Text += "<li><a href=\"Announcement.aspx?page=" + _page+ "\">"+ _page+"</a></li>"; 
                }
                if((curPage -2) > 1)
                {
                    pages.Text = "<li><a>...</a></li>" + pages.Text;
                    pages.Text = "<li><a href=\"Announcement.aspx?page=1\">1</a></li>" + pages.Text; 
                }

            /*
             * Kết thúc
             */
            DataSet newer = FunctionsUtils.Database2.ExecuteQuery("select top 5 * from tbl_News where ID not in (select top 3 ID from tbl_News where slider != '' order by ID desc) order by id desc");
            if (newer != null && newer.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in newer.Tables[0].Rows)
                {
                    popular.Text += "<li><a href=\"Read.aspx?id=" + row[0].ToString() + "\">";
                    popular.Text += "<img src=\"Images/avatar/" + row[7].ToString() + "\"></a>";
                    popular.Text += "<h3><a href=\"Read.aspx?id=" + row[0].ToString() + "\">" + row[1].ToString() + "</a></h3>";//comments
                    popular.Text += "<span class=\"comments\">" + row[8].ToString() + "</span>";
                    popular.Text += "<span class=\"post-date\">" + row[5].ToString().Split(' ')[0] + "</span></li>";
                }
            }
            DataSet datarecent = FunctionsUtils.Database2.ExecuteQuery("select top 5 * from tbl_News order by Viewer desc");
            if (datarecent != null && datarecent.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in datarecent.Tables[0].Rows)
                {
                    recent.Text += "<li><a href=\"Read.aspx?id=" + row[0].ToString() + "\">";
                    recent.Text += "<img src=\"Images/avatar/" + row[7].ToString() + "\"></a>";
                    recent.Text += "<h3><a href=\"Read.aspx?id=" + row[0].ToString() + "\">" + row[1].ToString() + "</a></h3>";
                    recent.Text += "<span class=\"comments\">" + row[8].ToString() + "</span>";
                    recent.Text += "<span class=\"post-date\">" + row[5].ToString().Split(' ')[0] + "</span></li>";
                }
            }
        }
    }
}